package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.hbpa.dao.DaduiInforDao;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;

public class DaduiInforDaoImpl implements DaduiInforDao {

	public List<String> getAllDaduiName() {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		List<String> dds = null;
		try {
			con = ConnectionFactory.getConnection(); 
			String sql = "select dd from dd_infor order by id asc";
			dds = new ArrayList<String>();  
			
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next())
			{ 
				 dds.add(rs.getString("dd"));
			}
			return dds;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,st,con);
		}
		return null;
	}

	public boolean addDaduiName(String name) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into dd_infor(dd) values(?)");
			ps.setString(1, name); 
			
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean delDaduiByName(String dd) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from dd_infor where dd=?");
			ps.setString(1, dd);
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean checkDaduiInfor(String dd) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from dd_infor where dd = ?");
			ps.setString(1,dd);
			
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(count.trim().equals("1")){
					return true;
				}else{
					return false;
				}
			}
			 
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}
}
